﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Lambda
{
  public  class LinqShow
    {

        private List<Student> GetStudentList()
        {
            #region 初始化数据
            List<Student> studentList = new List<Student>()
            {
                new Student()
                {
                    Id=1,
                    Name="打兔子的猎人",
                    ClassId=2,
                    Age=35
                },
                new Student()
                {
                    Id=1,
                    Name="Alpha Go",
                    ClassId=2,
                    Age=23
                },
                 new Student()
                {
                    Id=1,
                    Name="白开水",
                    ClassId=2,
                    Age=27
                },
                 new Student()
                {
                    Id=1,
                    Name="狼牙道",
                    ClassId=2,
                    Age=26
                },
                new Student()
                {
                    Id=1,
                    Name="Nine",
                    ClassId=2,
                    Age=25
                },
                new Student()
                {
                    Id=1,
                    Name="Y",
                    ClassId=2,
                    Age=24
                },
                new Student()
                {
                    Id=1,
                    Name="小昶",
                    ClassId=2,
                    Age=21
                },
                 new Student()
                {
                    Id=1,
                    Name="yoyo",
                    ClassId=2,
                    Age=22
                },
                 new Student()
                {
                    Id=1,
                    Name="冰亮",
                    ClassId=2,
                    Age=34
                },
                 new Student()
                {
                    Id=1,
                    Name="瀚",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="毕帆",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="一点半",
                    ClassId=2,
                    Age=30
                },
                new Student()
                {
                    Id=1,
                    Name="小石头",
                    ClassId=2,
                    Age=28
                },
                new Student()
                {
                    Id=1,
                    Name="大海",
                    ClassId=2,
                    Age=30
                },
            };
            #endregion
            return studentList;
        }

        public void Show()
        {
            List<Student> studentList = this.GetStudentList();
            List<Student> studentListlessThan30 = new List<Student>();
            foreach (var item in studentList)
            {
                if (item.Age < 30)
                {
                    studentListlessThan30.Add(item);
                }
            }

            //where陈述句
            {
                var list = studentList.Where(s => s.Age < 30).ToList();
                foreach (var item in list)
                {
                    Console.WriteLine("Name={0}  Age={1}", item.Name, item.Age);
                }

            }


            // 利用自定义的扩展方法 
            {
                Console.WriteLine("********************");
                var list = studentList.TomWhere<Student>(s => s.Age < 30);//陈述句

                //Func<Student, bool> func = new Func<Student, bool>(s => s.Age < 30);
                //var list = ExtendShow.ElevenWhere<Student>(studentList,func);//陈述句
                foreach (var item in list)
                {
                    Console.WriteLine("Name={0}  Age={1}", item.Name, item.Age);
                }
            }

            //  linq

            {
                var list = from student in studentList
                    where student.Age < 30
                    select student;
            }


            {
               var  list2= from student in studentList
                           where student.Age>30
               select student;

               foreach (var item in list2)
               {
                   Console.WriteLine("Name={0}  Age={1}", item.Name, item.Age);
               }
            }


            {
                studentList.Where(s => s.Age < 30).Select(
                    s => new
                    {
                        helloage = s.Age,
                        classname = s.ClassId == 2 ? " VIP" : "拼命班"
                    }
                );
            }

            {
                var list =  from student in studentList
                    where student.Age < 30
                    select new
                    {
                        helloage = student.Age,
                        classname = student.ClassId == 2 ? " VIP" : "拼命班"
                    };

            }

            {
                var list2 = studentList.Where(s => s.Age > 30).Select(s => new
                    {
                        helloage = s.Age,
                        classname = s.ClassId == 2 ? " VIP" : "ss"

                    }
                ).OrderByDescending(s=>s.helloage).Skip(2).Take(5);
            }



        }
    }
}
